package com.gsx.service.impl;

import com.gsx.bean.Cart;
import com.gsx.bean.CartDetail;
import com.gsx.bean.InfoDetail;
import com.gsx.dao.CartDetailMapper;
import com.gsx.dao.CartMapper;
import com.gsx.service.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Authoe gsx
 * @Date 2021/04/08 上午 10:37
 **/
@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private CartDetailMapper cartDetailMapper;
    @Override
    public boolean setCart(Cart cart) {
        int insert = cartMapper.insert(cart);
        boolean b =  insert  > 0;
        if (b){
            List<InfoDetail> infoDetails = cart.getInfoDetails();
            for(InfoDetail infoDetail:infoDetails){
                CartDetail cartDetail = new CartDetail();
                cartDetail.setFkCartId(cart.getCartId());
                cartDetail.setFkInfoDetailId(infoDetail.getFkBasicInfoId());
                cartDetailMapper.insert(cartDetail);
            }
        }
        return b;
    }

    @Override
    public List<Cart> getListByUid(int id) {
        return cartMapper.selectListByUid(id);
    }



    @Override
    public List<List<Cart>> getListByCid(Integer[] id) {
        List<List<Cart>> carts=null;
        for (int i=0;i<id.length;i++){
            if (i==0){
                carts=new ArrayList<>();
            }
            carts.add(cartMapper.selectListByCid(id[i]));
        }
        return carts;
    }

}
